//
//  FirstLauchViewController.m
//  TTAddressBook
//
//  Created by WangJuan on 11-12-26.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "FirstLauchViewController.h"
#import "CommTools.h"
#import "AppDelegate.h"
#import "SafeClauseViewController.h"
#import "firstViewController.h"

@implementation FirstLauchViewController
@synthesize isPushed;
- (id)init{
    self = [super init];
    if (self) {
        self.isPushed = NO;
        isAlterShow = NO;
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}
- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle
-(void)loadView{
    imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
    imageScrollView.contentSize = CGSizeMake(320*5, 460);
    imageScrollView.showsVerticalScrollIndicator = NO;
    imageScrollView.showsHorizontalScrollIndicator = NO;
    imageScrollView.pagingEnabled = YES;
    imageScrollView.bounces = NO;
    imageScrollView.scrollsToTop = NO;
    imageScrollView.delegate = self;
    self.view = imageScrollView;
    [imageScrollView release];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    isChecked = YES;    //默认是同意隐私条款
    if (!isPushed) {
        self.navigationController.navigationBarHidden = YES;
       // [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
    }
    else
         imageScrollView.bounces = YES;
    
    //添加图1
    NSString* imageFilePath = [[NSBundle mainBundle] pathForResource:@"show1" ofType:@"png"];
    UIImage* image1 = [[UIImage alloc] initWithContentsOfFile:imageFilePath];
    UIImageView* imageview1 = [[UIImageView alloc] initWithImage:image1];
    [image1 release];
    imageview1.center = CGPointMake(160, 230);
    [imageScrollView addSubview:imageview1];
    [imageview1 release];
    
    //添加图2
    imageFilePath = [[NSBundle mainBundle] pathForResource:@"show2" ofType:@"png"];
    UIImage* image2 = [[UIImage alloc] initWithContentsOfFile:imageFilePath];
    UIImageView* imageview2 = [[UIImageView alloc] initWithImage:image2];
    [image2 release];
    imageview2.center = CGPointMake(480, 230);
    [imageScrollView addSubview:imageview2];
    [imageview2 release];
    
    //添加图3
    imageFilePath = [[NSBundle mainBundle] pathForResource:@"show3" ofType:@"png"];
    UIImage* image3 = [[UIImage alloc] initWithContentsOfFile:imageFilePath];
    UIImageView* imageview3 = [[UIImageView alloc] initWithImage:image3];
    [image3 release];
    imageview3.center = CGPointMake(800, 230);
    [imageScrollView addSubview:imageview3];
    [imageview3 release];
    
    //添加图4
    UIView* selfview = [[UIView alloc] initWithFrame:CGRectMake(1120-160, 0, 320, 460)];   
   
    NSString* image4FilePath = nil;
    //NSString* startBtnImagePath = nil;
    //if (1) 
    {
        image4FilePath = [[NSBundle mainBundle] pathForResource:@"show4" ofType:@"png"];
        //startBtnImagePath = [[NSBundle mainBundle] pathForResource:@"start_icon" ofType:@"png"];
    }
//    else
//    {
//        image4FilePath = [[NSBundle mainBundle] pathForResource:@"start_bg_04" ofType:@"png"];
//        startBtnImagePath = [[NSBundle mainBundle] pathForResource:@"start_skip" ofType:@"png"];
//        begin_regist = [[UIButton alloc] initWithFrame:CGRectMake(60, 315, 200, 40)];
//        begin_regist.tag = 13;
//        begin_regist.titleLabel.textColor = [UIColor whiteColor];
//        begin_regist.titleLabel.font = [UIFont boldSystemFontOfSize:17];
//        [begin_regist setTitle:@"好!开始使用Free信吧!" forState:UIControlStateNormal];
//        
//        [begin_regist setBackgroundImage:[UIImage imageNamed:@"new_activation_button.png"] forState:UIControlStateNormal];
//        [begin_regist setBackgroundImage:[UIImage imageNamed:@"new_activation_button_on.png"] forState:UIControlStateHighlighted];
//        [begin_regist setBackgroundImage:[UIImage imageNamed:@"new_activation_disabel.png"] forState:UIControlStateDisabled];
//        [begin_regist addTarget:self action:@selector(activateEventByButton:) forControlEvents:UIControlEventTouchUpInside];
//        [selfview addSubview:begin_regist];
//        [begin_regist release];
//        
//        isChecked = YES;
//        checkBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        checkBtn.frame = CGRectMake(25, 360, 38, 32);
//        [checkBtn setImage:[UIImage imageNamed:@"start_agreement_on.png"] forState:UIControlStateNormal];
//        [checkBtn addTarget:self action:@selector(checkButtonClick:) forControlEvents:UIControlEventTouchUpInside];
//        checkBtn.tag = 10;
//        [selfview addSubview:checkBtn];
//        
//        readLabel = [[UILabel alloc] initWithFrame:CGRectMake(55, 365, 98, 20)];
//        readLabel.font = [UIFont systemFontOfSize:14];
//        readLabel.backgroundColor = [UIColor clearColor];
//        readLabel.textColor = [UIColor whiteColor];
//        readLabel.text = @"已经阅读并同意";
//        [selfview addSubview:readLabel];
//        [readLabel release];
//        
//        UIButton* questionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        questionBtn.frame = CGRectMake(143, 355, 158, 40);
//        questionBtn.titleLabel.font = [UIFont systemFontOfSize:14];
//        [questionBtn setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
//        [questionBtn setTitle:@"《隐私条款和资费说明》" forState:UIControlStateNormal];
//        [questionBtn addTarget:self action:@selector(noteButtonClick:) forControlEvents:UIControlEventTouchUpInside];
//        questionBtn.tag = 12;
//        [selfview addSubview:questionBtn];        
//    }

    {
        UIImage* image4 = [[UIImage alloc] initWithContentsOfFile:image4FilePath];
        UIImageView *bg = [[UIImageView alloc]initWithImage:image4];
        [image4 release];
        bg.center = CGPointMake(160, 230);
        [selfview addSubview:bg];
        [selfview sendSubviewToBack:bg];
        [bg release];
    }
    if (isPushed) {
        UIView* view = [[UIView alloc] initWithFrame:CGRectMake(320*5, 0, 320, 460)];
        view.backgroundColor = [UIColor blackColor];
        [self.view addSubview:view];
        [view release];
    }
    else{
        imageFilePath = [[NSBundle mainBundle] pathForResource:@"denglu" ofType:@"png"];
        UIImage* image5 = [[UIImage alloc] initWithContentsOfFile:imageFilePath];
        UIImageView* imageview5 = [[UIImageView alloc] initWithImage:image5];
        [image5 release];
        imageview5.center = CGPointMake(800+640, 230);
        [imageScrollView addSubview:imageview5];
        [imageview5 release];
    }
    
//    UIButton* skipBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    skipBtn.frame = CGRectMake(200, 390, 114, 35);
//    [skipBtn setTitle:@"开始吧!!!" forState:UIControlStateNormal];
//    //UIImage* btnBackImage = [[UIImage alloc] initWithContentsOfFile:startBtnImagePath];
//    //[skipBtn setImage:btnBackImage forState:UIControlStateNormal];
//    //[btnBackImage release];
//    [skipBtn addTarget:self action:@selector(laterEvent) forControlEvents:UIControlEventTouchUpInside];
//    [selfview addSubview:skipBtn];
    
    [imageScrollView addSubview:selfview];
    [selfview release];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    //[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)activateEventByButton:(id)sender 
{
    if ( isChecked )
    {

    }
}

- (void)laterEventByButton:(id)sender 
{
    [self laterEvent];
}

- (void)laterEventByImage:(id)sender 
{
    [self laterEvent];
}

- (void)checkButtonClick:(id)sender 
{
    if ( isChecked )
    {
        isChecked = NO;
        [checkBtn setImage:[UIImage imageNamed:@"start_agreement.png"] forState:UIControlStateNormal];
        readLabel.text = @"请先阅读并确认";
        [begin_regist setEnabled:NO];
    }
    else
    {
        isChecked = YES;
        [checkBtn setImage:[UIImage imageNamed:@"start_agreement_on.png"] forState:UIControlStateNormal];
        readLabel.text = @"已经阅读并同意";
        [begin_regist setEnabled:YES];
    }
}

- (void)noteButtonClick:(id)sender 
{
    SafeClauseViewController *safeClauseView = [[SafeClauseViewController alloc] initWithNibName:@"SafeClauseViewController" bundle:nil];
    safeClauseView.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentModalViewController:safeClauseView animated:YES];
    [safeClauseView release];
}

- (void)laterEvent  //点击以后再说按钮
{
    if (isPushed) 
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
    else
    {
        firstViewController* firstView = [[firstViewController alloc] initWithNibName:@"firstViewController" bundle:nil];
        [self.navigationController pushViewController:firstView animated:YES];
        [firstView release];
    }
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)sender
{
    if (isPushed) {
        CGFloat offsetX = imageScrollView.contentOffset.x;
        if (offsetX<0) {
            [self.navigationController popViewControllerAnimated:YES];
        }
        if (offsetX>960+80) {
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
    else if (((UIScrollView*)sender).contentOffset.x>320*3+160) 
    {
        /*
        firstViewController* firstView = [[firstViewController alloc] initWithNibName:@"firstViewController" bundle:nil];
        [self.navigationController pushViewController:firstView animated:YES];
        [firstView release];
         */
        if (!isAlterShow) {
            isAlterShow = YES;
            imageScrollView.contentOffset=CGPointMake(960+320, imageScrollView.contentOffset.y);
            UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"保合宝客户端申请已经发送到您的用户名下，请登录 baohebao.com，予以确认。" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
            alert.tag = 100;
            [alert show];
            [alert release];
        }

    }
    
}

-(void) exit{
    exit(0);
}

#pragma mark - UIAlertViewDelegate

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    switch ( alertView.tag )
    {
        isAlterShow = NO;
        case 100:
        {
            if ( buttonIndex == 0 )
            {
               // [theAppDelegate showActivityView:YES text:@"保合宝，让我们一起见证互联网改变保险！"];
                [self performSelector:@selector(exit) withObject:nil afterDelay:1];
                
            }
        }
            break;
        case AfterAlertTag: //以后再说提示
        {
            if ( buttonIndex == 0 ) //以后再说
            {
                //进入主页面
                [theAppDelegate OnLoginView:NO];
            }
            else    //buttonIndex == 1  现在注册
            {
//                RegistrationViewController *registrationView = [[RegistrationViewController alloc] initWithNibName:@"RegistrationViewController" bundle:nil];
//                registrationView.PopToView = EPopToView_ReloadTab;
//                [self.navigationController pushViewController:registrationView animated:NO];
//                [registrationView release];
            }
        }
            break;
        case SmsNotSupportAlertTag: //不支持发送短信功能提示
        {
            if ( buttonIndex == 0 )
            {
                
            }
        }
            break;
        case ActivateAlertTag:
        {
            if ( buttonIndex == 1 )
            {
//                RegistrationViewController *registrationView = [[RegistrationViewController alloc] initWithNibName:@"RegistrationViewController" bundle:nil];
//                registrationView.PopToView = EPopToView_ReloadTab;
//                [self.navigationController pushViewController:registrationView animated:YES];
//                [registrationView release];
            }
        }
            break;
        default:
            break;
    }
}

#pragma mark - MFMessageComposeViewControllerDelegate

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    // Notifies users about errors associated with the interface
	switch (result) 
    {
		case MessageComposeResultCancelled:
        {
        }
			break;
		case MessageComposeResultSent:
        {

        }
			break;
		case MessageComposeResultFailed:
        {

        }
			break;
		default:
			break;
	}
    
	[self dismissModalViewControllerAnimated:YES];	
}

- (void)dealloc 
{
    [super dealloc];
}
@end
